package br.ufrj.dcc.smarthotel;

import java.util.LinkedList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/***
 * Activity que contem a descricao de um ponto turistico do menu Tour
 * @author Diego Xavier
 * @author Patricia Zudio
 */
public class SightDescriptionActivity extends Activity implements OnClickListener {
	
	private JSONObject sightDescription;
	//private final String url = "http://www.dcc.ufrj.br/~diegoxavier/tour.php";
	private double latitude;
	private double longitude;
	
	/***
     * Metodo chamado na criacao da Activity
     * @param savedInstanceState - Parametros para a inicializacao da Activity
     */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	//Chama o OnCreate da Classe Activity
        super.onCreate(savedInstanceState);
        
        //Recupera o bundle para pegar o parametro passado pela Activity
        Bundle bundle = this.getIntent().getExtras();
        
        //Pega o parametro passado pela Activity
        List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("itemId", Long.toString(bundle.getLong("itemId"))));
        
        //Recupera as informacoes sobre o ponto turistico passado por parametro
        this.sightDescription = Util.getInstance(this.getApplicationContext()).serverHttpGetObject(Constants.TOUR_URL + "/" +  Long.toString(bundle.getLong("itemId")), null);
        
        //Seta o layout relacionado a Activity e seu conteudo
        setContentView(R.layout.sightdescription);
        ImageView imgViewSight = (ImageView) findViewById(R.id.sightPhoto);
        TextView txtViewSightName = (TextView) findViewById(R.id.sightName);
        TextView txtViewDescription = (TextView) findViewById(R.id.sightDescription);
        Button btnGetDirections = (Button) findViewById(R.id.btnGetDirections);
        try {
        	imgViewSight.setImageBitmap(Util.getInstance(this.getApplicationContext()).downloadImage(this.sightDescription.getString("miniatureURL")));
        	txtViewSightName.setText(this.sightDescription.getString("name")); 
        	txtViewDescription.setText(this.sightDescription.getString("description"));
        	btnGetDirections.setOnClickListener(this);
        	
        	//Setando a latitude e a longitude do Ponto Turistico
        	this.latitude = this.sightDescription.getDouble("latitude");
        	this.longitude = this.sightDescription.getDouble("longitude");
        	
        } catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
    }
    
    /***
     * Metodo que trata o click do botao Ver Mapa
     * @param view - Objeto que foi clicado
     */
    public void onClick(View v) {
    	Bundle params = new Bundle();
		params.putDouble("latitude", this.latitude);
		params.putDouble("longitude", this.longitude);

		Intent it = new Intent(this, DirectionsActivity.class);
		it.putExtras(params);
		startActivity(it);
	}
}
